import axios from 'axios'
import {message, Modal} from 'antd';
import store from '../store'
import {getToken} from "./auth";


//127.0.0.1:8888/systemApi/logout
const isDev = process.env.NODE_ENV === 'development'
const service = axios.create({
    baseURL: isDev ? 'http://127.0.0.1:8880' : '',
    timeout: 15000, // request timeout
    headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        'Access-Control-Allow-Origin': '*',
        "accept": "application/json"
    }
});
//添加请求拦截器
service.interceptors.request.use((config) => {

    if (store.getState().user.token) {
        config.headers.Authorization = getToken();
    }

    return config;
}, function (error) {
    //对请求错误做些什么
    console.log(error);
    return Promise.reject(error);
});

//添加响应拦截器
service.interceptors.response.use((response) => {
    //对响应数据做些什么：
    const res = response.data;
    if (res.code !== 200) {
        message.success('This is a prompt message for success, ' +
            'and it will disappear in 10 seconds', 5);
        if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
            // Modal.confirm()
            //TODO   这个地方重新获取token
        }
        // return Promise.reject(new Error(res.message || "Error"));
        // resolve(res);
        return res;
    } else {
        return res;
    }

}, function (error) {
    console.log('err' + error);
    message.error(error.message, 5);


    return Promise.reject(error);

});

export default service
